package cn.ecut.session;

import cn.ecut.javabean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;


@WebServlet("/getSession")
public class GetSession extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 收集用户信息
        User user = new User() ;
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        user.setName( name );
        user.setPassword( password );
        // 理论上说，应该利用 JDBC 技术，去数据库中查找。但是这里做了一次简化

        // 利用 HttpServletRequest 获取 session
        HttpSession session = request.getSession();
        // 进行操作session
        session.setAttribute("user" , user );
        if( Objects.nonNull( user.getName() ) ){
            // 去成功页面
            response.sendRedirect("/success");
        }
    }
}
